﻿//------------------------------------------------------------
// Game Framework v3.x
// Copyright © 2013-2017 Jiang Yin. All rights reserved.
// Homepage: http://gameframework.cn/
// Feedback: mailto:jiangyin@gameframework.cn
//------------------------------------------------------------

using UnityEngine;

namespace UnityGameFramework.Runtime
{
    public class GameFrameworkComponent<T> : MonoBehaviour where T : MonoBehaviour
    {
        protected static T _instance;
        /**
          Returns the instance of this singleton.
       */
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = (T)FindObjectOfType(typeof(T));
                    if (_instance == null)
                    {

                        GameObject go = new GameObject("UpdateManager");
                        _instance = go.AddComponent<T>();
                    }
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        public virtual void Awake()
        {
            if (_instance == null)
            {
                _instance = this as T;
            }
            else
            {
                if (Debug.unityLogger.logEnabled)
                {
                    Debug.Log("Repeat XComponentSingleton" + typeof(T));
                }
            }
        }
    }
}